// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init()
const db = cloud.database();
// 云函数入口函数
// 删除对应的like表数据
exports.main = async (event, context) => {
  const wxContext = cloud.getWXContext()

  const classifyId = event.classifyId;
  const cp = event.cp;
  const openid = wxContext.OPENID;
  let result = await db.collection("like").where({
    classifyId,
    openid
  }).get();

  if(result.data.length){
    let like = result.data.find(el=>el.cp.id === cp.id);
    await db.collection("like").doc(like._id).remove();
    return {code:200,msg:"取消成功"}
  }else{
    return {code:500,msg:"取消失败"}
  }
}